﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.Security;

namespace Tebbee.UI.Client
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            

            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

            // look if any security information exists for this request

            if (HttpContext.Current.User != null)
            {

                // see if this user is authenticated, any authenticated cookie (ticket) exists for this user

                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {

                    // see if the authentication is done using FormsAuthentication

                    if (HttpContext.Current.User.Identity is FormsIdentity)
                    {

                        // Get the roles stored for this request from the ticket

                        // get the identity of the user

                        FormsIdentity identity = (FormsIdentity)HttpContext.Current.User.Identity;

                        // get the forms authetication ticket of the user

                        FormsAuthenticationTicket ticket = identity.Ticket;

                        // get the roles stored as UserData into the ticket

                        string[] roles = ticket.UserData.Split(',');

                        // create generic principal and assign it to the current request

                        HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(identity, roles);

                    }

                }

            }

        } 
    }
}